<?php

class Application_Model_dataDAO_PhieuDatHangDAO extends Zend_Db_Table_Abstract implements Application_Model_dataDAOInf_PhieuDatHangInf {
	protected $_name = "tb_phieudathang";
	protected static $_instance = null;
	
	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new self ();
		}
		return self::$_instance;
	}
	public function getAllPhieuDatHang() {
		$sql = $this->select ();
		$sql->where ( 'biHuyHayChua=?', 0 );
		$result = $this->fetchAll ( $sql );
		$this->getAdapter ()->closeConnection ();
		return $result;
		
	//		$result = $this->fetchAll ();
	//		$this->getAdapter ()->closeConnection ();
	//		return $result;
	}
	
	/**
	 * @param unknown_type $idPhieuDatHang
	 */
	public function getPhieuDatHang($idPhieuDatHang) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$phieuDatHang = new Application_Model_bean_PhieuDatHang ();
		
		try {
			$statement = "SELECT * FROM tb_phieudathang where maPhieuDatHang=" . $idPhieuDatHang;
			$result = $db->fetchRow ( $statement );
			$phieuDatHang->setGiaTri ( $result ['giaTri'] );
			$phieuDatHang->setNgayTao ( $result ['ngayTao'] );
			$phieuDatHang->setTrangThai ( $result ['trangThai'] );
			$phieuDatHang->setBiHuyHayChua ( $result ['biHuyHayChua'] );
			$phieuDatHang->setMaKhachHang ( $result ['maKhachHang'] );
			$phieuDatHang->setMaTaiKhoan ( $result ['maTaiKhoan'] );
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $phieuDatHang;
	}
	/**
	 * @param unknown_type $idTaiKhoan
	 */
	public function getAllPhieuHang($idTaiKhoan) {
		$phieuDatHang = array ();
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_phieudathang where maTaiKhoan=" . $idTaiKhoan;
			$result = $db->fetchAll ( $statement );
			if (count ( $result > 0 )) {
				$i = 0;
				foreach ( $result as $value ) {
					$phieu = new Application_Model_bean_PhieuDatHang ();
					$phieu->setMaPhieuDatHang ( $value ['maPhieuDatHang'] );
					$phieu->setGiaTri ( $value ['giaTri'] );
					$phieu->setNgayTao ( $value ['ngayTao'] );
					$phieu->setTrangThai ( $value ['trangThai'] );
					$phieu->setBiHuyHayChua ( $value ['biHuyHayChua'] );
					$phieu->setMaKhachHang ( $value ['maKhachHang'] );
					$phieu->setMaTaiKhoan ( $value ['maTaiKhoan'] );
					$phieuDatHang [$i] = $phieu;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $phieuDatHang;
	}
	/**
	 * @param unknown_type $idPhieuDatHang
	 * @param unknown_type $trangThai
	 */
	public function capNhatTrangThaiPhieuDatHang($idPhieuDatHang, $trangThai) {
		$result = false;
		try {
			$update = array ("trangThai" => $trangThai );
			$conditions [] = 'maPhieuDatHang=' . $idPhieuDatHang;
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$value = $db->update ( 'tb_phieudathang', $update, $conditions );
			if (count ( $value ) == 1)
				$result = true;
			$db->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $result;
	}
	/**
	 * @param unknown_type $idPhieuDatHang
	 */
	public function huyPhieuDatHang($idPhieuDatHang) {
		$result = false;
		try {
			$update = array ("biHuyHayChua" => 1 );
			$conditions [] = 'maPhieuDatHang=' . $idPhieuDatHang;
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$value = $db->update ( 'tb_phieudathang', $update, $conditions );
			if (count ( $value ) == 1)
				$result = true;
			$db->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $result;
	}
	
	public function getAllPhieuDatHangHuy() {
		$sql = $this->select ();
		$sql->where ( 'biHuyHayChua=?', 1 );
		$result = $this->fetchAll ( $sql );
		$this->getAdapter ()->closeConnection ();
		return $result;
	}
	/**
	 * @param unknown_type $idPhieuDatHang
	 */
	public function phucHoiPhieuDatHang($idPhieuDatHang) {
		$result = false;
		try {
			$update = array ("biHuyHayChua" => 0 );
			$conditions [] = 'maPhieuDatHang=' . $idPhieuDatHang;
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$value = $db->update ( 'tb_phieudathang', $update, $conditions );
			if (count ( $value ) == 1)
				$result = true;
			$db->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $result;
	}
	/**
	 * @param unknown_type $noiDungTimKiem
	 * @param unknown_type $tieuChiTimKiem
	 */
	public function timKiemPhieuDatHang($noiDungTimKiem, $tieuChiTimKiem) {
		$phieuDatHang = array ();
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_phieudathang where " . $tieuChiTimKiem . "=" . $noiDungTimKiem;
			$result = $db->fetchAll ( $statement );
			if (count ( $result > 0 )) {
				$i = 0;
				foreach ( $result as $value ) {
					$phieu = new Application_Model_bean_PhieuDatHang ();
					$phieu->setMaPhieuDatHang ( $value ['maPhieuDatHang'] );
					$phieu->setGiaTri ( $value ['giaTri'] );
					$phieu->setNgayTao ( $value ['ngayTao'] );
					$phieu->setTrangThai ( $value ['trangThai'] );
					$phieu->setBiHuyHayChua ( $value ['biHuyHayChua'] );
					$phieu->setMaKhachHang ( $value ['maKhachHang'] );
					$phieu->setMaTaiKhoan ( $value ['maTaiKhoan'] );
					$phieuDatHang [$i] = $phieu;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			//			echo $e->getMessage ();
			echo '---Nội dung tìm kiếm không hợp lệ';
		}
		return $phieuDatHang;
	}
	/**
	 * @param unknown_type $phieuDatHang
	 */
	public function themPhieuDatHang($giaTri, $ngayTao, $trangThai, $biHuyHayChua, $maKhachHang, $maTaiKhoan) {
		$result = 0;
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$data = array ("giaTri" => $giaTri, "ngayTao" => $ngayTao, "trangThai" => $trangThai, "biHuyHayChua" => $biHuyHayChua, "maKhachHang" => $maKhachHang,"maTaiKhoan"=>$maTaiKhoan);
			$db->insert ( 'tb_phieudathang', $data );
			$result = $db->lastInsertId ();
			$db->closeConnection ();
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $result;
	}
/**
	 * @param unknown_type $giaTri
	 * @param unknown_type $ngayTao
	 * @param unknown_type $trangThai
	 * @param unknown_type $biHuyHayChua
	 * @param unknown_type $maKhachHang
	 * @param unknown_type $maTaiKhoan
	 */
	public function suaPhieuDatHang($idPhieuDatHang,$giaTri, $ngayTao, $trangThai, $biHuyHayChua, $maKhachHang, $maTaiKhoan) {
		$result=false;
		try {
			$db=Application_Model_dataDAO_DBConnect::conn();
			$data = array ("giaTri" => $giaTri, "ngayTao" => $ngayTao, "trangThai" => $trangThai, "biHuyHayChua" => $biHuyHayChua, "maKhachHang" => $maKhachHang,"maTaiKhoan"=>$maTaiKhoan);
			$conditions[] = 'maPhieuDatHang='.$idPhieuDatHang;
			$value=$db->update('tb_phieudathang',$data,$conditions);
			if($value==1) $result=true;
			$db->closeConnection();
		} catch (Zend_Db_Exception  $e) {
			echo $e->getMessage();
		}
		
		return $result;
	}
/**
	 * 
	 */
	public function getAllPhieuDatHangChuaXuLy() {
		$phieuDatHang = array ();
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_phieudathang where trangThai=0 AND biHuyHayChua=0";
			$result = $db->fetchAll ( $statement );
			if (count ( $result > 0 )) {
				$i = 0;
				foreach ( $result as $value ) {
					$phieu = new Application_Model_bean_PhieuDatHang ();
					$phieu->setMaPhieuDatHang ( $value ['maPhieuDatHang'] );
					$phieu->setGiaTri ( $value ['giaTri'] );
					$phieu->setNgayTao ( $value ['ngayTao'] );
					$phieu->setTrangThai ( $value ['trangThai'] );
					$phieu->setBiHuyHayChua ( $value ['biHuyHayChua'] );
					$phieu->setMaKhachHang ( $value ['maKhachHang'] );
					$phieu->setMaTaiKhoan ( $value ['maTaiKhoan'] );
					$phieuDatHang [$i] = $phieu;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $phieuDatHang;
	}

/**
	 * 
	 */
	public function getAllPhieuDatHangDaXuLy() {
		$phieuDatHang = array ();
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$statement = "SELECT * FROM tb_phieudathang where trangThai=1 AND biHuyHayChua=0";
			$result = $db->fetchAll ( $statement );
			if (count ( $result > 0 )) {
				$i = 0;
				foreach ( $result as $value ) {
					$phieu = new Application_Model_bean_PhieuDatHang ();
					$phieu->setMaPhieuDatHang ( $value ['maPhieuDatHang'] );
					$phieu->setGiaTri ( $value ['giaTri'] );
					$phieu->setNgayTao ( $value ['ngayTao'] );
					$phieu->setTrangThai ( $value ['trangThai'] );
					$phieu->setBiHuyHayChua ( $value ['biHuyHayChua'] );
					$phieu->setMaKhachHang ( $value ['maKhachHang'] );
					$phieu->setMaTaiKhoan ( $value ['maTaiKhoan'] );
					$phieuDatHang [$i] = $phieu;
					$i ++;
				}
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		return $phieuDatHang;
	}


	/**
	 * 
	 */

}

?>